package com.monitor.message.manager;


import com.monitor.message.model.RedisModel;
import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.*;

/**
 * @Author tanlian
 * @Date 2018/9/26 16:11
 * @Desc
 */
@Component
public class RedisManager {

    @Autowired
    private JedisPool jedisPool;

    private static Jedis jedis = null;

    public Boolean insertObject(String key,Object data) {
        try(Jedis jedis =jedisPool.getResource()) {
            RedisModel model = new RedisModel();
            model.setAge(123);
            model.setName("tanlian");
            model.setIdCard("522730199606180036");
            ByteOutputStream os = new ByteOutputStream();
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(os);
            objectOutputStream.writeObject(model);
            byte [] v = os.getBytes();
            objectOutputStream.writeObject(key);
            byte [] k = os.getBytes();
            jedis.set(k,v);
            return Boolean.TRUE;
        }catch (Exception e){
            e.printStackTrace();
        }
        return Boolean.FALSE;
    }



    public String getValue(String key){
        jedis = jedisPool.getResource();
        return jedis.get(key);

    }
}
